// pages/laboratory/laboratory.js
const app = getApp()
var server = require('../../utils/server');
var that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tu: app.globalData.tupian,
    list: [],
    headerimg: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    that = this
    server.requestForm(false, 'get', 'cardKind', {
      sort:{
        ord:1 //排序
      },
      filter:{
        showState:1 //1显示   0不显示
      }
    }, function (e) {
      console.log(e)
      if (e.code == 0) {
        var cv = {
          createTime: "",
          kind: "自定义视频",
          lastModifyDate: "",
          picPath: "../img/zidingyishipin.png",
          showState: 0,
          _id: "5e6a0110682b3834406858e3",
          // bgm:''
        }
        var cc = {
          createTime: "",
          kind: "自定义",
          lastModifyDate: "",
          picPath: "../img/zidingyi.png",
          showState: 0,
          _id: "5e6a0110682b3834406858e3",
          // bgm:''
        }
        e.data.unshift(cc)
        e.data.unshift(cv)
        console.log(e.data)
        that.setData({
          list: e.data
        })
      } else {
        wx.showToast({
          title: res.message,
          icon: 'none',
          duration: 1000,
        });
      }
    })
    that.headerimg()
  },
  headerimg: function (e) {
    server.requestForm(false, 'get', 'sysconfig', {
      filter: {
        key: '小程序卡片模板标题图'
      }
    }, function (e) {
      console.log(e)
      if (e.code == 0) {
        if (e.data.length < 1) return;
        that.setData({
          headerimg: e.data[0].val
        })
      } else {
        wx.showToast({
          title: res.message,
          icon: 'none',
          duration: 1000,
        });
      }
    })
  },
  release: function (e) {
    if (app.globalData.userID == '') {
      wx.showToast({
        title: '请先去我的里授权登录',
        icon: 'none',
        duration: 1000,
      });
    } else {
      var picpath = e.currentTarget.dataset.picpath
      var cardKindID = e.currentTarget.dataset.id
      var kind = e.currentTarget.dataset.kind
      var bgm = e.currentTarget.dataset.bgm
      console.log(e)
      if (kind === '自定义') {
        console.log('zidingyi')
        wx.chooseImage({
          count: 9,
          sizeType: ['compressed'],
          // sizeType: ['original', 'compressed'],
          sourceType: ['album', 'camera'],
          success(res) {
            const tempFilePaths = res.tempFilePaths
            console.log("chooseImage", tempFilePaths)
            wx.navigateTo({
              url: '../release/release?picpath=' + tempFilePaths.join("|") + '&cardKindID=' + cardKindID + '&kind=' + kind + "&moren=" + '1',
            })
          }
        })
      } else if (kind === "自定义视频") {
        wx.chooseVideo({
          // sizeType: ['original', 'compressed'],
          compressed: true,
          maxDuration: 30,
          sourceType: ['album', 'camera'],
          success(res) {
            const tempFilePath = res.tempFilePath
            console.log("chooseVideo", tempFilePath)
            wx.navigateTo({
              url: '../release/release?picpath=' + tempFilePath + '&cardKindID=' + cardKindID + '&kind=' + kind + "&moren=" + '1',
            })
          }
        })
      } else {
        if(!bgm){
          console.log('bgm',bgm)
          wx.navigateTo({
            url: '../release/release?picpath=' + picpath + '&cardKindID=' + cardKindID + '&kind=' + kind,
          })
        }else{
          wx.navigateTo({
            url: '../release/release?picpath=' + picpath + '&cardKindID=' + cardKindID + '&kind=' + kind +'&bgm=' + bgm,
          })
        }
      }
    }
  },

  // cardtype:function(e){
  //   if (app.globalData.userID == '') {
  //     wx.showToast({
  //       title: '请先去我的里授权登录',
  //       icon: 'none',
  //       duration: 1000,
  //     });
  //   } else {
  //     var picpath = e.currentTarget.dataset.picpath
  //     var cardKindID = e.currentTarget.dataset.id
  //     var kind = e.currentTarget.dataset.kind
  //     console.log(e)
  //     wx.navigateTo({
  //       url: '../cardtype/cardtype?picpath=' + picpath + '&cardKindID=' + cardKindID + '&kind=' + kind,
  //     })
  //   }
  // },
  sy: function (e) {
    wx.reLaunch({
      url: '../index/index',
    })
  },
  fx: function (e) {
    wx.reLaunch({
      url: '../find/find',
    })
  },
  xx: function (e) {
    wx.reLaunch({
      url: '../news/news',
    })
  },
  my: function (e) {
    wx.reLaunch({
      url: '../mine/my/my',
    })
  }
})